package com.sanbi.erp.core.util;

import com.github.houbb.property.bs.PropertyBs;
import com.sanbi.erp.core.config.UserConfig;
import com.sanbi.erp.core.dto.GoodsDto;
import com.sanbi.erp.core.dto.ProductDto;
import com.sanbi.erp.core.system.Enums;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @desc 业务工具类
 * @author wanggang
 * @time 2022/7/15 18:46
 */
public class BizUtil {

    public static GoodsDto getGoodsByUserLevel(Enums.UserLevel userLevel, GoodsDto originGoods){
        //校验当前日期和优选日期相等并优选标识为1时商品为优选
//        originGoods.setIsBestToday(isBestToday(originGoods));
//        //校验昨今明商品
//        originGoods.setBestGoodsTime(BestGoodsTime(dateTime));

        //获取会员配置信息
        UserConfig userConfig = new UserConfig();
        PropertyBs.getInstance("user.properties").asBean(userConfig);

//        GoodsDto returnGoods=new GoodsDto();
//        BeanUtil.copyBean(originGoods,returnGoods);

        int retailDiscount = 0;//零售折扣
        int wholesaleDiscount = 0;//批发折扣
        int historyRetailDiscount = 0;//历史零售折扣
        int historyWholesaleDiscount = 0;//历史批发折扣

        //计算各层级会员零售及批发折扣
        switch (userLevel){
            case Star1:
                //如果是今日优选商品：
                // 1. 零售折扣 = 优选折扣 + 零售折扣；
                // 2. 批发折扣 = 优选折扣 + 批发折扣；
                // 3. 历史零售折扣 =零售折扣；
                // 4. 历史批发折扣 = 批发折扣；
                if(Enums.BestType.Today.getCode() == originGoods.getBestType())
                {
                    retailDiscount = userConfig.getBest() + userConfig.getStar1Retail();
                    wholesaleDiscount = userConfig.getBest() + userConfig.getStar1Batch();
                    historyRetailDiscount = userConfig.getStar1Retail();
                    historyWholesaleDiscount = userConfig.getStar1Batch();
                }else {
                    //如果是非今日优选商品：
                    // 1. 零售折扣 =零售折扣；
                    // 2. 批发折扣 = 批发折扣；
                    // 3. 历史零售折扣 = 优选折扣 + 零售折扣；
                    // 4. 历史批发折扣 = 优选折扣 + 批发折扣；
                    retailDiscount = userConfig.getStar1Retail();
                    wholesaleDiscount = userConfig.getStar1Batch();
                    historyRetailDiscount = userConfig.getBest() + userConfig.getStar1Retail();
                    historyWholesaleDiscount = userConfig.getBest() + userConfig.getStar1Batch();
                }
                break;
            case Star2:
                if(Enums.BestType.Today.getCode() == originGoods.getBestType())
                {
                    retailDiscount = userConfig.getBest() + userConfig.getStar2Retail();
                    wholesaleDiscount = userConfig.getBest() + userConfig.getStar2Batch();
                    historyRetailDiscount = userConfig.getStar2Retail();
                    historyWholesaleDiscount = userConfig.getStar2Batch();
                }else {
                    retailDiscount = userConfig.getStar2Retail();
                    wholesaleDiscount = userConfig.getStar2Batch();
                    historyRetailDiscount = userConfig.getBest() + userConfig.getStar2Retail();
                    historyWholesaleDiscount = userConfig.getBest() + userConfig.getStar2Batch();
                }
                break;
            case Star3:
                if(Enums.BestType.Today.getCode() == originGoods.getBestType())
                {
                    retailDiscount = userConfig.getBest() + userConfig.getStar3Retail();
                    wholesaleDiscount = userConfig.getBest() + userConfig.getStar3Batch();
                    historyRetailDiscount = userConfig.getStar3Retail();
                    historyWholesaleDiscount = userConfig.getStar3Batch();
                }else {
                    retailDiscount = userConfig.getStar3Retail();
                    wholesaleDiscount = userConfig.getStar3Batch();
                    historyRetailDiscount = userConfig.getBest() + userConfig.getStar3Retail();
                    historyWholesaleDiscount = userConfig.getBest() + userConfig.getStar3Batch();
                }
                break;
            case Star4:
                if(Enums.BestType.Today.getCode() == originGoods.getBestType())
                {
                    retailDiscount = userConfig.getBest() + userConfig.getStar4Retail();
                    wholesaleDiscount = userConfig.getBest() + userConfig.getStar4Batch();
                    historyRetailDiscount = userConfig.getStar4Retail();
                    historyWholesaleDiscount = userConfig.getStar4Batch();
                }else {
                    retailDiscount = userConfig.getStar4Retail();
                    wholesaleDiscount = userConfig.getStar4Batch();
                    historyRetailDiscount = userConfig.getBest() + userConfig.getStar4Retail();
                    historyWholesaleDiscount = userConfig.getBest() + userConfig.getStar4Batch();
                }
                break;
            case Star5:
                if(Enums.BestType.Today.getCode() == originGoods.getBestType())
                {
                    retailDiscount = userConfig.getBest() + userConfig.getStar5Retail();
                    wholesaleDiscount = userConfig.getBest() + userConfig.getStar5Batch();
                    historyRetailDiscount = userConfig.getStar5Retail();
                    historyWholesaleDiscount = userConfig.getStar5Batch();
                }else {
                    retailDiscount = userConfig.getStar5Retail();
                    wholesaleDiscount = userConfig.getStar5Batch();
                    historyRetailDiscount = userConfig.getBest() + userConfig.getStar5Retail();
                    historyWholesaleDiscount = userConfig.getBest() + userConfig.getStar5Batch();
                }
                break;
        }
        /**
         * divide(BigDecimal.valueOf(100),3,RoundingMode.HALF_UP) 当前除出来的总优惠率保留三位小数，超出三位第三位开始四舍五入
         * 如：94.45/100 = 0.9445 最后得出 0.945
         * 如：93.45/100 = 0.9345 最后得出 0.935
         */
        //set工具类的批发价（计算：市场价 x（总率 - 批发率）/总率）将计算结果四舍五入保留两位
        originGoods.setWholesalePrice(originGoods.getMarketPrice().multiply(BigDecimal.valueOf(100 - wholesaleDiscount).divide(BigDecimal.valueOf(100),3,RoundingMode.HALF_UP)).setScale(2,RoundingMode.HALF_UP));
        //set工具类的零售价（计算：市场价 x（总率 - 零售率）/总率）将计算结果四舍五入保留两位
        originGoods.setRetailPrice(originGoods.getMarketPrice().multiply(BigDecimal.valueOf(100 - retailDiscount).divide(BigDecimal.valueOf(100),3,RoundingMode.HALF_UP)).setScale(2,RoundingMode.HALF_UP));

        originGoods.setHistoryWholesalePrice(originGoods.getMarketPrice().multiply(BigDecimal.valueOf(100 - historyWholesaleDiscount).divide(BigDecimal.valueOf(100),3,RoundingMode.HALF_UP)).setScale(2,RoundingMode.HALF_UP));
        //set工具类的零售价（计算：市场价 x（总率 - 零售率）/总率）将计算结果四舍五入保留两位
        originGoods.setHistoryRetailPrice(originGoods.getMarketPrice().multiply(BigDecimal.valueOf(100 - historyRetailDiscount).divide(BigDecimal.valueOf(100),3,RoundingMode.HALF_UP)).setScale(2,RoundingMode.HALF_UP));

        return originGoods;
    }

    public static ProductDto getProductByUserLevel(Enums.UserLevel userLevel, ProductDto originProduct){

        //获取会员配置信息
        UserConfig userConfig = new UserConfig();
        PropertyBs.getInstance("user.properties").asBean(userConfig);

        int retailDiscount = 0;//零售折扣
        int wholesaleDiscount = 0;//批发折扣
        int historyRetailDiscount = 0;//历史零售折扣
        int historyWholesaleDiscount = 0;//历史批发折扣

        //计算各层级会员零售及批发折扣
        switch (userLevel){
            case Star1:
                //如果是今日优选商品：
                // 1. 零售折扣 = 优选折扣 + 零售折扣；
                // 2. 批发折扣 = 优选折扣 + 批发折扣；
                // 3. 历史零售折扣 =零售折扣；
                // 4. 历史批发折扣 = 批发折扣；
                if(Enums.BestType.Today.getCode() == originProduct.getBestType())
                {
                    retailDiscount = userConfig.getBest() + userConfig.getStar1Retail();
                    wholesaleDiscount = userConfig.getBest() + userConfig.getStar1Batch();
                    historyRetailDiscount = userConfig.getStar1Retail();
                    historyWholesaleDiscount = userConfig.getStar1Batch();
                }else {
                    //如果是非今日优选商品：
                    // 1. 零售折扣 =零售折扣；
                    // 2. 批发折扣 = 批发折扣；
                    // 3. 历史零售折扣 = 优选折扣 + 零售折扣；
                    // 4. 历史批发折扣 = 优选折扣 + 批发折扣；
                    retailDiscount = userConfig.getStar1Retail();
                    wholesaleDiscount = userConfig.getStar1Batch();
                    historyRetailDiscount = userConfig.getBest() + userConfig.getStar1Retail();
                    historyWholesaleDiscount = userConfig.getBest() + userConfig.getStar1Batch();
                }
                break;
            case Star2:
                if(Enums.BestType.Today.getCode() == originProduct.getBestType())
                {
                    retailDiscount = userConfig.getBest() + userConfig.getStar2Retail();
                    wholesaleDiscount = userConfig.getBest() + userConfig.getStar2Batch();
                    historyRetailDiscount = userConfig.getStar2Retail();
                    historyWholesaleDiscount = userConfig.getStar2Batch();
                }else {
                    retailDiscount = userConfig.getStar2Retail();
                    wholesaleDiscount = userConfig.getStar2Batch();
                    historyRetailDiscount = userConfig.getBest() + userConfig.getStar2Retail();
                    historyWholesaleDiscount = userConfig.getBest() + userConfig.getStar2Batch();
                }
                break;
            case Star3:
                if(Enums.BestType.Today.getCode() == originProduct.getBestType())
                {
                    retailDiscount = userConfig.getBest() + userConfig.getStar3Retail();
                    wholesaleDiscount = userConfig.getBest() + userConfig.getStar3Batch();
                    historyRetailDiscount = userConfig.getStar3Retail();
                    historyWholesaleDiscount = userConfig.getStar3Batch();
                }else {
                    retailDiscount = userConfig.getStar3Retail();
                    wholesaleDiscount = userConfig.getStar3Batch();
                    historyRetailDiscount = userConfig.getBest() + userConfig.getStar3Retail();
                    historyWholesaleDiscount = userConfig.getBest() + userConfig.getStar3Batch();
                }
                break;
            case Star4:
                if(Enums.BestType.Today.getCode() == originProduct.getBestType())
                {
                    retailDiscount = userConfig.getBest() + userConfig.getStar4Retail();
                    wholesaleDiscount = userConfig.getBest() + userConfig.getStar4Batch();
                    historyRetailDiscount = userConfig.getStar4Retail();
                    historyWholesaleDiscount = userConfig.getStar4Batch();
                }else {
                    retailDiscount = userConfig.getStar4Retail();
                    wholesaleDiscount = userConfig.getStar4Batch();
                    historyRetailDiscount = userConfig.getBest() + userConfig.getStar4Retail();
                    historyWholesaleDiscount = userConfig.getBest() + userConfig.getStar4Batch();
                }
                break;
            case Star5:
                if(Enums.BestType.Today.getCode() == originProduct.getBestType())
                {
                    retailDiscount = userConfig.getBest() + userConfig.getStar5Retail();
                    wholesaleDiscount = userConfig.getBest() + userConfig.getStar5Batch();
                    historyRetailDiscount = userConfig.getStar5Retail();
                    historyWholesaleDiscount = userConfig.getStar5Batch();
                }else {
                    retailDiscount = userConfig.getStar5Retail();
                    wholesaleDiscount = userConfig.getStar5Batch();
                    historyRetailDiscount = userConfig.getBest() + userConfig.getStar5Retail();
                    historyWholesaleDiscount = userConfig.getBest() + userConfig.getStar5Batch();
                }
                break;
        }
        /**
         * divide(BigDecimal.valueOf(100),3,RoundingMode.HALF_UP) 当前除出来的总优惠率保留三位小数，超出三位第三位开始四舍五入
         * 如：94.45/100 = 0.9445 最后得出 0.945
         * 如：93.45/100 = 0.9345 最后得出 0.935
         */
        //set工具类的批发价（计算：规格货品市场价 x（总率 - 批发率）/总率）将计算结果四舍五入保留两位
        originProduct.setWholesalePrice(originProduct.getPrice().multiply(BigDecimal.valueOf(100 - wholesaleDiscount).divide(BigDecimal.valueOf(100),3,RoundingMode.HALF_UP)).setScale(2,RoundingMode.HALF_UP));
        //set工具类的零售价（计算：规格货品市场价 x（总率 - 零售率）/总率）将计算结果四舍五入保留两位
        originProduct.setRetailPrice(originProduct.getPrice().multiply(BigDecimal.valueOf(100 - retailDiscount).divide(BigDecimal.valueOf(100),3,RoundingMode.HALF_UP)).setScale(2,RoundingMode.HALF_UP));

        originProduct.setHistoryWholesalePrice(originProduct.getPrice().multiply(BigDecimal.valueOf(100 - historyWholesaleDiscount).divide(BigDecimal.valueOf(100),3,RoundingMode.HALF_UP)).setScale(2,RoundingMode.HALF_UP));
        //set工具类的零售价（计算：规格货品市场价 x（总率 - 零售率）/总率）将计算结果四舍五入保留两位
        originProduct.setHistoryRetailPrice(originProduct.getPrice().multiply(BigDecimal.valueOf(100 - historyRetailDiscount).divide(BigDecimal.valueOf(100),3,RoundingMode.HALF_UP)).setScale(2,RoundingMode.HALF_UP));

        return originProduct;
    }

    public static void main(String[] args) {
        BigDecimal bigDecimal = new BigDecimal(100);
        BigDecimal bigDecimal1 = bigDecimal.multiply(BigDecimal.valueOf(100 - 5.55555).divide(BigDecimal.valueOf(100),3,RoundingMode.HALF_UP)).setScale(2, RoundingMode.HALF_UP);
        System.out.println("结果：" + bigDecimal1);
    }
}
